﻿using <%=dal_namespace%>;
using <%=idal_namespace%>;
using <%=ibll_namespace%>;
using <%=model_namespace%>;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using Util.Excel;

namespace <%=namespace%>
{
    public class <%=class_name%>: <%=ibll_class_name%>
    {
        #region 模块
        private readonly <%=idal_class_name%> _dal;
        private readonly IExcelAssistant _excel;
        public <%=class_name%>(<%=idal_class_name%> dal, IExcelAssistant excel)
        {
            _dal = dal;
            _excel = excel;
        }
        #endregion

        #region 查询

        /// <summary>
        /// 根据键值查询单个信息
        /// </summary>
        /// <param name="<%=key%>">键值</param>
        /// <returns></returns>
        public <%=model_name%> Get(<%=datatype.parse(key_type)%> <%=key%>)
        {
            return _dal.GetById(<%=key%>);
        }

        /// <summary>
        /// 分页查询批量信息
        /// </summary>
        <%for(var i = 0; i < fields.length; i++) {%>/// <param name="<%=fields[i].column_name%>"><%=fields[i].column_comment%></param>
        <%}%>/// <param name="page">页码</param>
        /// <param name="page_size">每页显示的条数</param>
        /// <returns></returns>
        public Tuple<List<<%=model_name%>>, int> ListPagin(<%for(var i = 0; i < fields.length; i++) {%><%=datatype.parse(fields[i].data_type)%> <%=fields[i].column_name%>, <%}%>int page, int page_size)
        {
            ISugarQueryable<<%=model_name%>> queryable = _dal.CreateQueryable();
            <%for(var i = 0; i < fields.length; i++) {%>queryable = queryable.WhereIF(<%if(datatype.parse(fields[i].data_type)=="string"){%>!string.IsNullOrEmpty(<%=fields[i].column_name%>)<%}else{%><%=fields[i].column_name%>!=null<%}%>,p => p.<%=fields[i].column_name%> == <%=fields[i].column_name%>);
            <%}%>
            PageModel pm = new PageModel
            {
                PageIndex = page,
                PageSize = page_size
            };
            Tuple<List<<%=model_name%>>, int> result = _dal.GetPageListAndTotal(queryable, pm);
            return result;
        }

        /// <summary>
        /// 分页查询批量信息
        /// </summary>
        <%for(var i = 0; i < fields.length; i++) {%>/// <param name="<%=fields[i].column_name%>"><%=fields[i].column_comment%></param>
        <%}%>/// <returns></returns>
        public List<<%=model_name%>> List(<%for(var i = 0; i < fields.length; i++) {%><%if(i>0){%>, <%}%><%=datatype.parse(fields[i].data_type)%> <%=fields[i].column_name%><%}%>)
        {
            Expressionable<<%=model_name%>> queryable = Expressionable.Create<<%=model_name%>>();
            <%for(var i = 0; i < fields.length; i++) {%>queryable = queryable.AndIF(<%if(datatype.parse(fields[i].data_type)=="string"){%>!string.IsNullOrEmpty(<%=fields[i].column_name%>)<%}else{%><%=fields[i].column_name%>!=null<%}%>,p => p.<%=fields[i].column_name%> == <%=fields[i].column_name%>);
            <%}%>
            List<<%=model_name%>> result = _dal.GetList(queryable.ToExpression());
            return result;
        }

        #endregion

        #region 提交

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public bool Insert(<%=model_name%> model)
        {
            model.create_date = DateTime.Now;
            return _dal.Insert(model);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public bool Update(<%=model_name%> model)
        {
            model.edit_date = DateTime.Now;
            return _dal.UpdateExcludeColumns(model, "creater", "create_date") == 1;
        }

        /// <summary>
        ///  删除
        /// </summary>
        /// <param name="<%=key%>_arr">键值</param>
        /// <returns></returns>
        public bool Delete(params <%=datatype.parse(key_type)%>[] <%=key%>_arr)
        {
            return _dal.Delete(<%=key%>_arr);
        }

        /// <summary>
        /// 获得要导出的数据的Excel流
        /// </summary>
        <%for(var i = 0; i < fields.length; i++) {%>/// <param name="<%=fields[i].column_name%>"><%=fields[i].column_comment%></param>
        <%}%>/// <returns></returns>
        public byte[] Export(<%for(var i = 0; i < fields.length; i++) {%><%if(i>0){%>, <%}%><%=datatype.parse(fields[i].data_type)%> <%=fields[i].column_name%><%}%>)
        {
            List<<%=model_name%>> result = List(<%for(var i = 0; i < fields.length; i++) {%><%if(i>0){%>, <%}%><%=fields[i].column_name%><%}%>);
            return _excel.Collection2ExcelBytes(result, "Sheet1");
        }
        #endregion
    }
}
